<?php
namespace app\api\controller;

use app\common\controller\Base;
use app\common\exception\ParameterException;
use app\common\logic\Pay as PayLogic;
use think\Loader;
use think\Log;
use think\Request;

/**
 * @title 支付模块接口
 * @description 接口说明
 * @group 接口分组
 * @header name:token require:1 default: desc:token
 * @param name:user_type type:int require:1 default:1 other: desc:状态(1.会员登陆，2.商家，3.代理商，4.省级代理商，5.内部员工)
 */
class Pay extends Base
{
    /**
 * @title 测试用户支付
 * @description 接口说明
 * @author WCJ
 * @url /api/pay/pay
 * @method POST
 *
 * @param name:shopid type:string require:1 default:0 other: desc:商店id
 * @param name:money type:decimal require:1 default:0 other: desc:金额
 * @param name:pay_password type:string require:1 default:0 other: desc:支付密码
 * @param name:type type:string require:1 default:0 other: desc:状态1.为余额2.现金支付3.支付宝4.微信5.银行卡
 *
 *
 */
    public function pay(Request $request){
        if($request->isPost()) {

            $userid = $this->uid;
            $shopid = $request->post('shopid');
            $money = $request->post('money');
            $pay_password =$request->post("pay_password");
            $type = $request->post('type');
            $validate = Loader::validate("User");

            if (!$validate->scene("pay")->check(['user_id' => $userid, 'shopid' => $shopid,'money'=>$money,'type'=>$type])) {
                throw new ParameterException(['code' => 201,'errorCode' => 404, 'msg' => $validate->getError()]);
                die();
            }
            $paylogic=new PayLogic();
            $pay_password=md5($pay_password);
            $yy_paypassword=$paylogic->yy_pay_password($userid,$pay_password);
            if(!$yy_paypassword){
                throw new ParameterException(['code' => 201,'errorCode' => 404, 'msg' => '支付密码错误!']);
                die();
            }

            $payno= $paylogic->pay($userid,$shopid,$money,$type);
            if($payno['code']!=200){
                throw new ParameterException(['code' => 201,'errorCode' => 404, 'msg' => $payno['msg']]);
                die();
            }else {
                $datas= ['code' => 200, 'msg' => '支付成功'];

                if($this->new_token){
                    $datas["new_token"]=$this->new_token;
                }
                return $datas;
                die();
            }
        }
    }


    /**
     * @title 正式用户支付
     * @description 接口说明
     * @author WCJ
     * @url /api/pay/zf_pay
     * @method POST
     *
     * @param name:shopid type:string require:1 default:0 other: desc:商店id
     * @param name:money type:decimal require:1 default:0 other: desc:金额
     * @param name:pay_password type:string require:1 default:0 other: desc:支付密码
     * @param name:type type:string require:1 default:0 other: desc:状态1.为余额2.现金支付3.支付宝4.微信5.银行卡
     *
     *
     */
    public function zf_pay(Request $request){
        if($request->isPost()) {

            $userid = $this->uid;
            $shopid = $request->post('shopid');
            $money = $request->post('money');
            $pay_password =$request->post("pay_password");
            $type = $request->post('type');
            $validate = Loader::validate("User");

            if (!$validate->scene("pay")->check(['user_id' => $userid, 'shopid' => $shopid,'money'=>$money,'type'=>$type])) {
                throw new ParameterException(['code' => 201,'errorCode' => 404, 'msg' => $validate->getError()]);
                die();
            }
            $paylogic=new PayLogic();
            $pay_password=md5($pay_password);
            if($type==1){
            $yy_paypassword=$paylogic->yy_pay_password($userid,$pay_password);
            if(!$yy_paypassword){
                throw new ParameterException(['code' => 201,'errorCode' => 404, 'msg' => '支付密码错误!']);
                die();
            }
            }

            $payno= $paylogic->pay_by($userid,$shopid,$money,$type);
            if($payno['code']!=200){
                throw new ParameterException(['code' => 201,'errorCode' => 404, 'msg' => $payno['msg']]);
                die();
            }else {
                $datas=$payno;
                  
                if($this->new_token){
                    $datas["new_token"]=$this->new_token;
                }
                return $datas;
                die();
            }
        }
    }



}